import 'package:enjoy_plus_01/utils/http.dart';
import 'package:flutter/material.dart';
import 'package:toastification/toastification.dart';

import 'components/home_list.dart';
import 'components/home_nav.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _IndexState();
}

class _IndexState extends State<HomePage> {
  // // 测试Dio
  // getData() async {
  //   final dio = Dio();
  //   // var res = await dio.get('https://live-api.itheima.net/announcement');
  //   // 与鸿蒙不同的点在于，dart中更推荐使用类构造对象而不是字面量对象
  //   var res = await httpClass.request(requestParam: RequestParamModel(url: '/announcement'));
  //   print(res.toString());
  //   // Future.delayed(const Duration(seconds: 0),(){
  //   //   toastification.show(
  //   //   context: context,
  //   //   title: res.data.toString(),
  //   //   autoCloseDuration: const Duration(seconds: 3),
  //   //   showProgressBar: false,
  //   //   showCloseButton: false,
  //   //   icon: const Icon(Icons.ac_unit_sharp),
  //   //   backgroundColor: Colors.pink
  //   //   );
  //   // });
  // }
  List notifyList = [];
  
  @override
  void initState() {
    super.initState();
    // getData();
    getNotifyList();
  }

  getNotifyList()async{
    var res = await httpClass.request(requestParam: RequestParamModel(url: '/announcement'));
    if(res['code']!=10000)
      toastification.showError(context: context, title: '数据获取失败');
    setState(() {
      notifyList = res['data'];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(40, 85, 145, 175),
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        scrollDirection: Axis.vertical,
        padding: const EdgeInsets.all(10),
        children: [
          // 导航条
          const HomeNav(),
          // 中间广告图
          ClipRRect(
            borderRadius: BorderRadius.circular(10.0),
            child: Image.asset('images/banner@2x.jpg'),
          ),
          // 社区公告
          // 如果组件涉及传值，不能给组件前面加const
          HomeList(notifyList: notifyList)
        ],
      ),
    );
  }
}
